package by.dkusch.aircompany.webapp.page.base.common;

import java.util.Iterator;
import java.util.Set;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.injection.Injector;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;

import by.dkusch.aircompany.model.tables.UserGrant;
import by.dkusch.aircompany.model.tables.UserProfile;
import by.dkusch.aircompany.model.tables.enums.FormType;
import by.dkusch.aircompany.services.UserService;

public class GrantDataProvider extends SortableDataProvider<UserGrant, SingularAttribute<UserGrant, ?>> {

	private static final long serialVersionUID = 1L;

	@Inject
	private UserService service;

	private UserProfile profile;

	public GrantDataProvider(UserProfile profile) {
		super();
		Injector.get().inject(this);
		this.profile = profile;

	}

	@Override
	public Iterator<UserGrant> iterator(long first, long count) {
		return grantList().iterator();
	}

	@Override
	public long size() {
		return FormType.values().length;
	}

	@Override
	public IModel<UserGrant> model(UserGrant entity) {
		return new CompoundPropertyModel<UserGrant>(entity);
	}

	private Set<UserGrant> grantList() {
		this.profile.setUserGrant(service.getGrants(profile));
		return this.profile.getUserGrant();
	}
}
